﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class SanPham_DAO
    {
        private static SanPham_DAO m_instance = null;

        public static SanPham_DAO Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new SanPham_DAO();
                return m_instance;
            }
        }

        /// <summary>
        /// Lấy toàn bộ sản phẩm
        /// </summary>
        /// <returns></returns>
        public DataTable LayToanBoSanPham()
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAY_TOANBO_SANPHAM", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                //cmd.Parameters.Add("@makhachhang", SqlDbType.Char, 10);
                //cmd.Parameters["@makhachhang"].Value = id;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);

                return table;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Lấy toàn bộ Mã Sản Phẩm
        /// </summary>
        /// <returns></returns>
        public DataTable LayToanBoMaSanPham()
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAY_TOANBO_MASANPHAM", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                //cmd.Parameters.Add("@makhachhang", SqlDbType.Char, 10);
                //cmd.Parameters["@makhachhang"].Value = id;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);

                return table;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Xóa sản phẩm khi biết mã sản phẩm
        /// </summary>
        /// <param name="masp"></param>
        /// <returns></returns>
        public bool XoaSanPham(string masp)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("REMOVE_SANPHAM", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@masanpham", SqlDbType.NVarChar, 10);
                cmd.Parameters["@masanpham"].Value = masp;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        ///   Tìm kiếm thông tin sản phẩm
        /// </summary>
        /// 
        /// 
        public DataTable TimKiemSanPham (string _id, string _name, string _type , string _dongia, string _slt)
        {
            DataTable result = new DataTable();
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("FIND_SANPHAM", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@MaSanPham", SqlDbType.NVarChar, 40);
                cmd.Parameters["@MaSanPham"].Value = _id;

                cmd.Parameters.Add("@TenSanPham", SqlDbType.NVarChar, 100);
                cmd.Parameters["@TenSanPham"].Value = _name;

                cmd.Parameters.Add("@TenLoaiSanPham", SqlDbType.NVarChar, 100);
                cmd.Parameters["@TenLoaiSanPham"].Value = _type;

                cmd.Parameters.Add("@DonGia", SqlDbType.NVarChar, 100);
                cmd.Parameters["@DonGia"].Value = "";

                cmd.Parameters.Add("@SoLuongTon", SqlDbType.NVarChar, 100);
                cmd.Parameters["@SoLuongTon"].Value = "";

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(result);
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// Cập nhật thông tin sản phẩm
        /// </summary>
        /// <param name="masp"></param>
        /// <param name="tensp"></param>
        /// <param name="maloaisp"></param>
        /// <param name="dongia"></param>
        /// <param name="sl"></param>
        /// <returns></returns>
        public bool CapNhatSanPham(string masp, string tensp, string maloaisp, long dongia, int sl)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("UPDATE_SANPHAM", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@masanpham", SqlDbType.NVarChar, 10);
                cmd.Parameters["@masanpham"].Value = masp;
                cmd.Parameters.Add("@tensanpham", SqlDbType.NVarChar, 40);
                cmd.Parameters["@tensanpham"].Value = tensp;
                cmd.Parameters.Add("@maloaisanpham", SqlDbType.NVarChar, 10);
                cmd.Parameters["@maloaisanpham"].Value = maloaisp;
                cmd.Parameters.Add("@dongia", SqlDbType.Money);
                cmd.Parameters["@dongia"].Value = dongia;
                cmd.Parameters.Add("@soluongton", SqlDbType.Int);
                cmd.Parameters["@soluongton"].Value = sl;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Thêm sản phẩm mới
        /// </summary>
        /// <param name="tensp"></param>
        /// <param name="malsp"></param>
        /// <param name="dongia"></param>
        /// <param name="soluong"></param>
        /// <returns></returns>
        public bool ThemSanPhamMoi(string tensp, string malsp, long dongia, int soluong)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("ADD_SANPHAM_MOI", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@tensanpham", SqlDbType.NVarChar, 40);
                cmd.Parameters["@tensanpham"].Value = tensp;
                cmd.Parameters.Add("@maloaisanpham", SqlDbType.NVarChar, 10);
                cmd.Parameters["@maloaisanpham"].Value = malsp;
                cmd.Parameters.Add("@dongia", SqlDbType.Money);
                cmd.Parameters["@dongia"].Value = dongia;
                cmd.Parameters.Add("@soluongton", SqlDbType.Int);
                cmd.Parameters["@soluongton"].Value = soluong;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Lấy thông tin sản phẩm bằng mã sản phẩm
        /// </summary>
        /// <param name="masp"></param>
        /// <returns></returns>
        public DataTable LayThongTinSanPham(string masp)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAYSANPHAMBYID", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@masanpham", SqlDbType.NVarChar, 10);
                cmd.Parameters["@masanpham"].Value = masp;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);

                return table;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
